<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺id" prop="mid">
        <el-input v-model="queryParams.mid" placeholder="请输入店铺id" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="用户id" prop="uid">
        <el-input v-model="queryParams.uid" placeholder="请输入用户id" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="下单时间" prop="orderTime">
        <el-date-picker clearable
          v-model="queryParams.orderTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择下单时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="预约时间" prop="reservationTime">
        <el-date-picker clearable
          v-model="queryParams.reservationTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择预约时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="支付方式" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择支付方式" clearable style="width: 180px;">
          <el-option v-for="dict in pay_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable style="width: 150px;">
          <el-option v-for="dict in order_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['ordersinfo:orders:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['ordersinfo:orders:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['ordersinfo:orders:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['ordersinfo:orders:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单号" align="center" prop="id" />
      <!-- <el-table-column label="核销码图片" align="center" prop="verificationImg" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.verificationImg" :width="50" :height="50"/>
        </template>
</el-table-column> -->
      <el-table-column label="店铺" align="center" prop="mid">
        <template #default="scope">
          {{ getMerchantName(scope.row.mid) }}
        </template>
      </el-table-column>
      <el-table-column label="用户id" align="center" prop="uid" />
      <el-table-column label="服务详情" align="center" prop="serviceId" >
        <template #default="scope">
          {{ getServiceName(scope.row.serviceId) }}
        </template>
      </el-table-column>
      <el-table-column label="服务类型" align="center" prop="serviceTypeId">
        <template #default="scope">
          {{ getTypeName(scope.row.serviceTypeId) }}
        </template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="orderTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="预约时间" align="center" prop="reservationTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.reservationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="实付金额" align="center" prop="price" />
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center" prop="payType">
        <template #default="scope">
          <dict-tag :options="pay_type" :value="scope.row.payType" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="使用时间" align="center" prop="useTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.useTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="订单状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="order_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['ordersinfo:orders:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['ordersinfo:orders:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改订单信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="ordersRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="核销码" prop="verificationCode">
          <el-input v-model="form.verificationCode" placeholder="请输入核销码" />
        </el-form-item>
        <el-form-item label="核销码图片" prop="verificationImg">
          <image-upload v-model="form.verificationImg" />
        </el-form-item>
        <el-form-item label="店铺id" prop="mid">
          <el-input v-model="form.mid" placeholder="请输入店铺id" />
        </el-form-item>
        <el-form-item label="用户id" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="服务详情id" prop="serviceId">
          <el-input v-model="form.serviceId" placeholder="请输入服务详情id" />
        </el-form-item>
        <el-form-item label="服务类型" prop="serviceTypeId">
          <el-select v-model="form.serviceTypeId" placeholder="请选择服务类型">
            <el-option v-for="type in fuwutypeList" :key="type.id" :label="type.type" :value="type.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="下单时间" prop="orderTime">
          <el-date-picker clearable v-model="form.orderTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择下单时间">
          </el-date-picker>
        </el-form-item>
        
        <el-form-item label="预约时间" prop="reservationTime">
          <el-date-picker clearable v-model="form.reservationTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择预约时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单车辆id" prop="carId">
          <el-input v-model="form.carId" placeholder="请输入订单车辆id" />
        </el-form-item>
        <el-form-item label="平台优惠券id" prop="platformCouponId">
          <el-input v-model="form.platformCouponId" placeholder="请输入平台优惠券id" />
        </el-form-item>
        <el-form-item label="商家优惠券id" prop="shopCouponId">
          <el-input v-model="form.shopCouponId" placeholder="请输入商家优惠券id" />
        </el-form-item>
        <el-form-item label="实付金额" prop="price">
          <el-input v-model="form.price" placeholder="请输入实付金额" />
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker clearable v-model="form.payTime" type="date" value-format="YYYY-MM-DD" placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付方式" prop="payType">
          <el-select v-model="form.payType" placeholder="请选择支付方式">
            <el-option v-for="dict in pay_type" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="使用时间" prop="useTime">
          <el-date-picker clearable v-model="form.useTime" type="date" value-format="YYYY-MM-DD" placeholder="请选择使用时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择订单状态">
            <el-option v-for="dict in order_status" :key="dict.value" :label="dict.label"
              :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Orders">
import { listOrders, getOrders, delOrders, addOrders, updateOrders } from "@/api/ordersinfo/orders"
import { listMerchant } from "@/api/merchant/merchant"
import { listFuwudetails } from "@/api/fuwu/fuwudetails"
import { listFuwutype} from "@/api/fuwu/fuwutype"
const { proxy } = getCurrentInstance()
const { pay_type, order_status } = proxy.useDict('pay_type', 'order_status')

const ordersList = ref([])
const merchantList = ref([])
const fuwudetailsList = ref([])
const fuwutypeList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    mid: null,
    uid: null,
    serviceTypeId: null,
    orderTime: null,
    reservationTime: null,
    payType: null,
    status: null,
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询订单信息列表 */
function getList() {
  loading.value = true
  listOrders(queryParams.value).then(response => {
    ordersList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}
/** 查询商户列表 */
function getList2() {
  loading.value = true
  listMerchant().then(response => {
    merchantList.value = response.rows
  })
}
/** 查询服务详情列表 */
function getList3() {
  loading.value = true
  listFuwudetails().then(response => {
    fuwudetailsList.value = response.rows
  })
}
/** 查询服务类型列表 */
function getList4() {
  loading.value = true
  listFuwutype().then(response => {
    fuwutypeList.value = response.rows
  })
}
// 根据商户id获取商户名称
function getMerchantName(merchantId) {
  const merchantItem = merchantList.value.find(merchant => merchant.id === merchantId);
  return merchantItem ? merchantItem.name : '';
}
// 根据服务详情id获取服务名称
function getServiceName(serviceId) {
  const serviceItem = fuwudetailsList.value.find(service => service.id === serviceId);
  return serviceItem? serviceItem.name : '';
}
//根据服务类型id获取服务类型名称
function getTypeName(serviceTypeId) {
  const typeItem = fuwutypeList.value.find(type => type.id === serviceTypeId);
  return typeItem ? typeItem.type : '';
}


// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    verificationCode: null,
    verificationImg: null,
    mid: null,
    uid: null,
    serviceId: null,
    serviceTypeId: null,
    orderTime: null,
    reservationTime: null,
    carId: null,
    platformCouponId: null,
    shopCouponId: null,
    price: null,
    payTime: null,
    payType: null,
    useTime: null,
    status: null,
    deleted: null
  }
  proxy.resetForm("ordersRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}



/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加订单信息"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getOrders(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改订单信息"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["ordersRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateOrders(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addOrders(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除订单信息编号为"' + _ids + '"的数据项？').then(function () {
    return delOrders(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => { })
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('ordersinfo/orders/export', {
    ...queryParams.value
  }, `orders_${new Date().getTime()}.xlsx`)
}

getList()
getList2()
getList3()
getList4()
</script>
